﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class KhoaViewModel : INotifyPropertyChanged
    {
        public KhoaViewModel()
        {
            RefreshKhoa();
            this.PropertyChanged += (s, e) => {
                if (insertCurKhoa != null)
                    insertCurKhoa.RaiseCanExecuteChanged();
                if (saveCurKhoa != null)
                    saveCurKhoa.RaiseCanExecuteChanged();
                if (delCurKhoa != null)
                    delCurKhoa.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<KHOA> khoas;
        public IEnumerable<KHOA> Khoas
        {
            get { return this.khoas; }
            set
            {
                this.khoas = value;
                OnPropertyChange("Khoas");
            }
        }


        private KHOA currentKhoa = new KHOA();

        public KHOA CurrentKhoa
        {
            get { return this.currentKhoa; }
            set
            {
                this.currentKhoa = value;
                OnPropertyChange("CurrentKhoa");
            }
        }
        
       
        #endregion

        #region refreshKhoa
        private void RefreshKhoa()
        {
            
            this.ServiceClient.getAllKhoaCompleted += (s, e) =>
            {
                this.Khoas = e.Result;
            };
            this.ServiceClient.getAllKhoaAsync();
        }
        #endregion
        
        #region Save
        private RelayCommand saveCurKhoa;
        public ICommand SaveCurKhoa
        {
            get { return saveCurKhoa ?? (saveCurKhoa = new RelayCommand(() => SaveKhoa(), () => this.currentKhoa != null && this.currentKhoa.ID != 0)); }
        }

        private void SaveKhoa()
        {
            ServiceClient.saveKhoaAsync(CurrentKhoa);
            RefreshKhoa();
        }

        
        #endregion

        #region del
        private RelayCommand delCurKhoa;
        public ICommand DelCurKhoa
        {
            get { return delCurKhoa ?? (delCurKhoa = new RelayCommand(() => DelKhoa(), () => this.currentKhoa != null && this.currentKhoa.ID != 0)); }
        }

        private void DelKhoa()
        {
            ServiceClient.deleteKhoaAsync(CurrentKhoa);
            RefreshKhoa();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurKhoa;
        public ICommand InsertCurKhoa
        {
            get { return insertCurKhoa ?? (insertCurKhoa = new RelayCommand(() => InsertKhoa(), () => this.currentKhoa != null)); }
        }

        private void InsertKhoa()
        {
            ServiceClient.insertKhoaAsync(CurrentKhoa);
            RefreshKhoa();
        }
        #endregion
        
    }
}
